{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "97fe9283-7e3a-476c-ad54-ff298b52874f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json\n",
    "import pandas as pd\n",
    "import demjson\n",
    "from jsonpath import jsonpath"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "711d10a4-77b4-42b6-ad8e-7e6c8f8f0c8a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>公历年份</th>\n",
       "      <th>公历月份</th>\n",
       "      <th>公历日期</th>\n",
       "      <th>公历周</th>\n",
       "      <th>公历一年中的第几天</th>\n",
       "      <th>星期几</th>\n",
       "      <th>是否为周末</th>\n",
       "      <th>是否为工作日</th>\n",
       "      <th>节假日</th>\n",
       "      <th>其它节假日</th>\n",
       "      <th>节假日调休</th>\n",
       "      <th>是否为节日当天</th>\n",
       "      <th>是否为法定节假日</th>\n",
       "      <th>是否为假期节假日</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>364</th>\n",
       "      <td>2021</td>\n",
       "      <td>202101</td>\n",
       "      <td>20210101</td>\n",
       "      <td>202053</td>\n",
       "      <td>1</td>\n",
       "      <td>星期五</td>\n",
       "      <td>非周末</td>\n",
       "      <td>非工作日</td>\n",
       "      <td>元旦</td>\n",
       "      <td>元旦</td>\n",
       "      <td>非节假日调休</td>\n",
       "      <td>节日当天</td>\n",
       "      <td>法定节假日</td>\n",
       "      <td>假期节假日</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>363</th>\n",
       "      <td>2021</td>\n",
       "      <td>202101</td>\n",
       "      <td>20210102</td>\n",
       "      <td>202053</td>\n",
       "      <td>2</td>\n",
       "      <td>星期六</td>\n",
       "      <td>周末</td>\n",
       "      <td>非工作日</td>\n",
       "      <td>元旦</td>\n",
       "      <td>元旦</td>\n",
       "      <td>非节假日调休</td>\n",
       "      <td>非节日当天</td>\n",
       "      <td>非法定节假日</td>\n",
       "      <td>假期节假日</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>362</th>\n",
       "      <td>2021</td>\n",
       "      <td>202101</td>\n",
       "      <td>20210103</td>\n",
       "      <td>202053</td>\n",
       "      <td>3</td>\n",
       "      <td>星期日</td>\n",
       "      <td>周末</td>\n",
       "      <td>非工作日</td>\n",
       "      <td>元旦</td>\n",
       "      <td>元旦</td>\n",
       "      <td>非节假日调休</td>\n",
       "      <td>非节日当天</td>\n",
       "      <td>非法定节假日</td>\n",
       "      <td>假期节假日</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>361</th>\n",
       "      <td>2021</td>\n",
       "      <td>202101</td>\n",
       "      <td>20210104</td>\n",
       "      <td>202101</td>\n",
       "      <td>4</td>\n",
       "      <td>星期一</td>\n",
       "      <td>非周末</td>\n",
       "      <td>工作日</td>\n",
       "      <td>非节假日</td>\n",
       "      <td>非节假日</td>\n",
       "      <td>非节假日调休</td>\n",
       "      <td>非节日当天</td>\n",
       "      <td>非法定节假日</td>\n",
       "      <td>非假期节假日</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>360</th>\n",
       "      <td>2021</td>\n",
       "      <td>202101</td>\n",
       "      <td>20210105</td>\n",
       "      <td>202101</td>\n",
       "      <td>5</td>\n",
       "      <td>星期二</td>\n",
       "      <td>非周末</td>\n",
       "      <td>工作日</td>\n",
       "      <td>非节假日</td>\n",
       "      <td>非节假日</td>\n",
       "      <td>非节假日调休</td>\n",
       "      <td>非节日当天</td>\n",
       "      <td>非法定节假日</td>\n",
       "      <td>非假期节假日</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2021</td>\n",
       "      <td>202112</td>\n",
       "      <td>20211227</td>\n",
       "      <td>202152</td>\n",
       "      <td>361</td>\n",
       "      <td>星期一</td>\n",
       "      <td>非周末</td>\n",
       "      <td>工作日</td>\n",
       "      <td>非节假日</td>\n",
       "      <td>非节假日</td>\n",
       "      <td>非节假日调休</td>\n",
       "      <td>非节日当天</td>\n",
       "      <td>非法定节假日</td>\n",
       "      <td>非假期节假日</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2021</td>\n",
       "      <td>202112</td>\n",
       "      <td>20211228</td>\n",
       "      <td>202152</td>\n",
       "      <td>362</td>\n",
       "      <td>星期二</td>\n",
       "      <td>非周末</td>\n",
       "      <td>工作日</td>\n",
       "      <td>非节假日</td>\n",
       "      <td>非节假日</td>\n",
       "      <td>非节假日调休</td>\n",
       "      <td>非节日当天</td>\n",
       "      <td>非法定节假日</td>\n",
       "      <td>非假期节假日</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2021</td>\n",
       "      <td>202112</td>\n",
       "      <td>20211229</td>\n",
       "      <td>202152</td>\n",
       "      <td>363</td>\n",
       "      <td>星期三</td>\n",
       "      <td>非周末</td>\n",
       "      <td>工作日</td>\n",
       "      <td>非节假日</td>\n",
       "      <td>非节假日</td>\n",
       "      <td>非节假日调休</td>\n",
       "      <td>非节日当天</td>\n",
       "      <td>非法定节假日</td>\n",
       "      <td>非假期节假日</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2021</td>\n",
       "      <td>202112</td>\n",
       "      <td>20211230</td>\n",
       "      <td>202152</td>\n",
       "      <td>364</td>\n",
       "      <td>星期四</td>\n",
       "      <td>非周末</td>\n",
       "      <td>工作日</td>\n",
       "      <td>非节假日</td>\n",
       "      <td>非节假日</td>\n",
       "      <td>非节假日调休</td>\n",
       "      <td>非节日当天</td>\n",
       "      <td>非法定节假日</td>\n",
       "      <td>非假期节假日</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2021</td>\n",
       "      <td>202112</td>\n",
       "      <td>20211231</td>\n",
       "      <td>202152</td>\n",
       "      <td>365</td>\n",
       "      <td>星期五</td>\n",
       "      <td>非周末</td>\n",
       "      <td>工作日</td>\n",
       "      <td>非节假日</td>\n",
       "      <td>非节假日</td>\n",
       "      <td>非节假日调休</td>\n",
       "      <td>非节日当天</td>\n",
       "      <td>非法定节假日</td>\n",
       "      <td>非假期节假日</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>365 rows × 14 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     公历年份    公历月份      公历日期     公历周 公历一年中的第几天  星期几 是否为周末 是否为工作日   节假日 其它节假日  \\\n",
       "364  2021  202101  20210101  202053         1  星期五   非周末   非工作日    元旦    元旦   \n",
       "363  2021  202101  20210102  202053         2  星期六    周末   非工作日    元旦    元旦   \n",
       "362  2021  202101  20210103  202053         3  星期日    周末   非工作日    元旦    元旦   \n",
       "361  2021  202101  20210104  202101         4  星期一   非周末    工作日  非节假日  非节假日   \n",
       "360  2021  202101  20210105  202101         5  星期二   非周末    工作日  非节假日  非节假日   \n",
       "..    ...     ...       ...     ...       ...  ...   ...    ...   ...   ...   \n",
       "4    2021  202112  20211227  202152       361  星期一   非周末    工作日  非节假日  非节假日   \n",
       "3    2021  202112  20211228  202152       362  星期二   非周末    工作日  非节假日  非节假日   \n",
       "2    2021  202112  20211229  202152       363  星期三   非周末    工作日  非节假日  非节假日   \n",
       "1    2021  202112  20211230  202152       364  星期四   非周末    工作日  非节假日  非节假日   \n",
       "0    2021  202112  20211231  202152       365  星期五   非周末    工作日  非节假日  非节假日   \n",
       "\n",
       "      节假日调休 是否为节日当天 是否为法定节假日 是否为假期节假日  \n",
       "364  非节假日调休    节日当天    法定节假日    假期节假日  \n",
       "363  非节假日调休   非节日当天   非法定节假日    假期节假日  \n",
       "362  非节假日调休   非节日当天   非法定节假日    假期节假日  \n",
       "361  非节假日调休   非节日当天   非法定节假日   非假期节假日  \n",
       "360  非节假日调休   非节日当天   非法定节假日   非假期节假日  \n",
       "..      ...     ...      ...      ...  \n",
       "4    非节假日调休   非节日当天   非法定节假日   非假期节假日  \n",
       "3    非节假日调休   非节日当天   非法定节假日   非假期节假日  \n",
       "2    非节假日调休   非节日当天   非法定节假日   非假期节假日  \n",
       "1    非节假日调休   非节日当天   非法定节假日   非假期节假日  \n",
       "0    非节假日调休   非节日当天   非法定节假日   非假期节假日  \n",
       "\n",
       "[365 rows x 14 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "year='2021'\n",
    "cn='1'\n",
    "size='366'\n",
    "\n",
    "url = 'https://api.apihubs.cn/holiday/get?year=' + year + '&cn=' + cn + '&size=' + size\n",
    "\n",
    "res = requests.get(url)\n",
    "json_data = demjson.decode(res.text)\n",
    "year_list = jsonpath(json_data, '$..year')\n",
    "month_list = jsonpath(json_data, '$..month')\n",
    "date_list = jsonpath(json_data, '$..date')\n",
    "week_list = jsonpath(json_data, '$..yearweek')\n",
    "yearday_list = jsonpath(json_data, '$..yearday')\n",
    "week_cn_list = jsonpath(json_data, '$..week_cn')\n",
    "weekend_cn_list = jsonpath(json_data, '$..weekend_cn')\n",
    "workday_cn_list = jsonpath(json_data, '$..workday_cn')\n",
    "holiday_cn_list = jsonpath(json_data, '$..holiday_cn')\n",
    "holiday_or_cn_list = jsonpath(json_data, '$..holiday_or_cn')\n",
    "holiday_overtime_cn_list = jsonpath(json_data, '$..holiday_overtime_cn')\n",
    "holiday_today_cn_list = jsonpath(json_data, '$..holiday_today_cn')\n",
    "holiday_legal_cn_list = jsonpath(json_data, '$..holiday_legal_cn')\n",
    "holiday_recess_cn_list = jsonpath(json_data, '$..holiday_recess_cn')\n",
    "\n",
    "list_content = [year_list,month_list,date_list,week_list,yearday_list,week_cn_list,weekend_cn_list,workday_cn_list,\\\n",
    "                holiday_cn_list,holiday_or_cn_list,holiday_overtime_cn_list,holiday_today_cn_list,holiday_legal_cn_list,holiday_recess_cn_list]\n",
    "df_jsonpath = pd.DataFrame(list_content)\n",
    "df_jsonpath = df_jsonpath.T\n",
    "df_jsonpath.rename(columns={\n",
    "                                0:'公历年份',\n",
    "                                1:'公历月份',\n",
    "                                2:'公历日期',\n",
    "                                3:'公历周',\n",
    "                                4:'公历一年中的第几天',\n",
    "                                5:'星期几',\n",
    "                                6:'是否为周末',\n",
    "                                7:'是否为工作日',\n",
    "                                8:'节假日',\n",
    "                                9:'其它节假日',\n",
    "                                10:'节假日调休',\n",
    "                                11:'是否为节日当天',\n",
    "                                12:'是否为法定节假日',\n",
    "                                13:'是否为假期节假日',\n",
    "                                },inplace=True)\n",
    "df_jsonpath.sort_values(by=['公历日期'],ascending=True,inplace=True)\n",
    "df_jsonpath"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2739ba36-5094-4ce4-ae05-1da90317896e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成 ./2021年节假日.xlsx 成功！！\n"
     ]
    }
   ],
   "source": [
    "filename = f'./{year}年节假日.xlsx'\n",
    "df_jsonpath.to_excel(filename,index=False)\n",
    "print(f'生成 {filename} 成功！！')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "495e85f3-5ee6-4023-9df6-bfa965fed38d",
   "metadata": {},
   "source": [
    "### 接口地址：https://api.apihubs.cn/holiday/get\n",
    "### 接口查询页面：http://www.apihubs.cn/#/holiday\n",
    "\n",
    "## 返回字段说明\n",
    "- year 公历年份\n",
    "- month 公历月份\n",
    "- date 公历日期\n",
    "- yearweek 公历一年中的第几周，注意这里的年份是ISO-8601周编号年份，始终以周一至周日为一周。如需获取7天为一周直接使用年份中的天数除7即可。\n",
    "- yearday 公历一年中的第几天\n",
    "- lunar_year 农历年份\n",
    "- lunar_month 农历月份\n",
    "- lunar_date 农历日期\n",
    "- lunar_yearday 农历一年中的第几天\n",
    "- week 星期几\n",
    "- weekend 是否为周末\n",
    "- workday 是否为工作日（包含调休在内需要上班的日子）\n",
    "- holiday 节假日，这里使用两位数字枚举表示节假日，其中特殊数字10表示非节假日，特殊数字99表示全部节假日\n",
    "- holiday_or 其他节假日，枚举与节假日相同，表示同一天中的另一个节日，如 2020-10-01\n",
    "- holiday_overtime 节假日调休，枚举与节假日相同\n",
    "- holiday_today 是否为节日当天\n",
    "- holiday_legal 是否为法定节假日（三倍工资）\n",
    "- holiday_recess 是否为假期节假日（节日是否放假）"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
